<!--  -->
<template>
  <div>
    <cu-custom
      :needBack="true"
      bgColor="bgNormal"
      topTitle="申请列表"
      titleClass="commTitleWhite"
      backIconColor="#fff"
    ></cu-custom>
    <view :style="{ height: getHeight() }" class="content">
      <tki-tree
        ref="tkitree"
        :range="range"
        rangeKey="target"
        :defaultIcon="img1"
        :currentIcon="img2"
        @confirm="goList"
      />
    </view>
  </div>
</template>

<script>
import { getTargetManagement } from "@/api/report.js";
import tkiTree from "@/components/sy-tree-number/tki-tree.vue";

export default {
  data() {
    return {
      img1: require("@/static/images/xiala1.png"),
      img2: require("@/static/images/xiala2.png"),
      range: [],
    };
  },
  onShow() {
    this.getList();
  },
  methods: {
    getIdsById(id, tree) {
      const node = this.findNodeById(id, tree); // 根据id查找对应的节点
      if (node) {
        return this.getAllIds(node); // 返回当前节点下的所有子节点id集合
      }
      return [];
    },

    findNodeById(id, tree) {
      for (const node of tree) {
        // 遍历数组中的每一个节点
        if (node.id === id) {
          return node; // 如果当前节点的id与指定id匹配，则返回当前节点
        }
        if (node.children) {
          // 如果当前节点有子节点
          const result = this.findNodeById(id, node.children); // 递归查找子节点
          if (result) {
            return result; // 如果找到了匹配的节点，则返回该节点
          }
        }
      }
      return null; // 如果未找到匹配的节点，则返回null
    },

    getAllIds(node) {
      const ids = [node.id]; // 将当前节点的id添加到集合中
      if (node.children) {
        // 如果当前节点有子节点
        for (const child of node.children) {
          // 遍历所有子节点
          ids.push(...this.getAllIds(child)); // 递归获取子节点的id并添加到集合中
        }
      }
      return ids;
    },

    getHeight() {
      return `calc(100vh - ${this.CustomBar}px)`;
    },
    goList(item) {
      console.log(item, "123");
      // let ids = this.getIdsById(item.id, this.range);
      let ids = [item.id];
      console.log(ids);
      uni.navigateTo({
        url: "/packageB/applicationList/index?id=" + ids.toString(),
      });
    },

    async getList() {
      let res = await getTargetManagement({
					socialOrganizationTypeId: uni.getStorageSync(
						"mySocialOrganizationTypeId"
					),
				});
      let tree = res.data;
      this.changeKey(tree,123);
	  console.log(tree)
      this.range = tree;
    },

    changeKey(arr) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].childs && arr[i].childs.length > 0) {
          arr[i].children = arr[i].childs;
          this.changeKey(arr[i].childs);
        }
      }
    },
  },
  components: {
    tkiTree,
  },
};
</script>
<style scoped>
.content {
  overflow-y: scroll;
}
</style>